using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using SNARF.Framework.ActiveRecord;
using NHibernate.Classic;


namespace SNARF.Framework.ActiveRecord
{
    class ActiveRecordInterceptor : IInterceptor
    {

        #region IInterceptor Members

        public void AfterTransactionBegin(ITransaction tx)
        {
           
        }

        public void AfterTransactionCompletion(ITransaction tx)
        {
            
        }

        public void BeforeTransactionCompletion(ITransaction tx)
        {
           
        }

        public int[] FindDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)
        {
            return null;
        }

        public object Instantiate(Type type, object id)
        {
            return null;
        }

        public object IsUnsaved(object entity)
        {
            return null;
        }

        public void OnDelete(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
        {
            if (this.GetCallBackEntity(entity) != null)
                this.GetCallBackEntity(entity).OnDeleting();
                
        }

        public bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)
        {
            if (this.GetCallBackEntity(entity) != null)
                this.GetCallBackEntity(entity).OnSaving();
            return true;

        }

        public bool OnLoad(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
        {
            
            if (this.GetCallBackEntity(entity) != null)
                this.GetCallBackEntity(entity).OnLoading(id);

            return true;
        }

        public bool OnSave(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
        {

            if (this.GetCallBackEntity(entity) != null)
                this.GetCallBackEntity(entity).OnSaving();

            return true;
        }


        public void PostFlush(System.Collections.ICollection entities)
        {
           
        }

        public void PreFlush(System.Collections.ICollection entities)
        {
            
        }

        public void SetSession(ISession session)
        {
            
        }

        #endregion

        private IActiveRecordCallBack GetCallBackEntity(object entity)
        {
            IActiveRecordCallBack arEntity = (entity as IActiveRecordCallBack);
            return arEntity;
        }

    }
}
